﻿// ImageWnd.cpp : 实现文件
//

#include "stdafx.h"
#include "ImageWnd.h"

CNHGDIPlusInit g_NHGDIplusInit;

// CImageWnd

IMPLEMENT_DYNAMIC(CImageWnd, CWnd)

CImageWnd::CImageWnd()
{
	m_pBitmap = NULL;
	m_hBuffer = NULL;
}

CImageWnd::~CImageWnd()
{
	if (m_pBitmap != NULL)
	{
		delete m_pBitmap;
		m_pBitmap = NULL;
	}
	if (m_hBuffer != NULL)
	{
		::GlobalFree(m_hBuffer);
		m_hBuffer = NULL;
	}
}


BEGIN_MESSAGE_MAP(CImageWnd, CWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CImageWnd 消息处理程序
void CImageWnd::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CRect rc;
	GetClientRect(&rc);

	Gdiplus::Graphics graphics(dc.m_hDC);

	if(m_pBitmap != NULL)
	{
		graphics.DrawImage(m_pBitmap, 0, 0, rc.Width(), rc.Height());
	}
}

void CImageWnd::SetImage(LPCTSTR lpszImageFile)
{
	if (m_pBitmap != NULL)
	{
		delete m_pBitmap;
		m_pBitmap = NULL;
	}
	m_pBitmap = CNHGDIPlusInit::LoadImageFromFile(lpszImageFile);
	Invalidate();
}

void CImageWnd::SetImage(UINT uImageID, LPCTSTR pResourceType)
{
	if (m_pBitmap != NULL)
	{
		delete m_pBitmap;
		m_pBitmap = NULL;
	}
	m_pBitmap = CNHGDIPlusInit::LoadImageFromID(uImageID, pResourceType);
	Invalidate();
}

void CImageWnd::SetImage(const BYTE *pImageBuf, const DWORD dwSize)
{
	if (m_pBitmap != NULL)
	{
		delete m_pBitmap;
		m_pBitmap = NULL;
	}
	
	if (m_hBuffer != NULL)
	{
		::GlobalFree(m_hBuffer);
		m_hBuffer = NULL;
	}

	m_hBuffer = ::GlobalAlloc(GMEM_FIXED, dwSize);
	if (m_hBuffer)
	{
		memcpy(m_hBuffer, pImageBuf, dwSize);

		IStream* pStream = NULL;
		if (::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream) == S_OK)
		{
			m_pBitmap = Gdiplus::Bitmap::FromStream(pStream);
			pStream->Release();
		}
	}

	Invalidate();
}

int CImageWnd::GetEncoderClsid(LPCTSTR szFormat, CLSID &clsid)
{
	UINT num= 0;
	UINT size= 0;
	Gdiplus::ImageCodecInfo* pImageCodecInfo= NULL;
	Gdiplus::GetImageEncodersSize(&num, &size);
	if(size== 0)
	{
		return -1;
	}
	pImageCodecInfo= (Gdiplus::ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo== NULL)
	{
		return -1;
	}
	Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
	for(UINT j=0; j<num; ++j)
	{
		if(_wcsicmp(pImageCodecInfo[j].MimeType, szFormat)== 0)
		{
			clsid= pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;
		}
	}
	free(pImageCodecInfo);
	return -1;
}

void CImageWnd::SaveImage(LPCTSTR szFilePath)
{
	const wchar_t *pExt = wcsrchr(szFilePath, L'.');
	if (pExt != NULL)
	{
		pExt++;
	}
	else
	{
		pExt = L"";
	}

//{557CF400-1A04-11D3-9A73-0000F81EF32E}-BMP
//{557CF401-1A04-11D3-9A73-0000F81EF32E}-JPEG
//{557CF402-1A04-11D3-9A73-0000F81EF32E}-GIF
//{557CF405-1A04-11D3-9A73-0000F81EF32E}-TIFF
//{557CF406-1A04-11D3-9A73-0000F81EF32E}-PNG
	//image/bmp CLSID
	//CLSID imgClsid = {0x557CF400, 0x1A04, 0x11D3, {0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E}};
	//image/jpeg CLSID
	//CLSID imgClsid = {0x557CF401, 0x1A04, 0x11D3, {0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E}};
	//image/gif CLSID
	//CLSID imgClsid = {0x557CF402, 0x1A04, 0x11D3, {0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E}};
	//image/tiff CLSID
	//CLSID imgClsid = {0x557CF405, 0x1A04, 0x11D3, {0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E}};
	//image/png CLSID
	//CLSID imgClsid = {0x557CF406, 0x1A04, 0x11D3, {0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E}};

	// 初始化为BMP的CLSID
	CLSID imgClsid = {0x557CF400, 0x1A04, 0x11D3, {0x9A, 0x73, 0x00, 0x00, 0xF8, 0x1E, 0xF3, 0x2E}};
	if (_wcsicmp(L"BMP", pExt) == 0
		|| _wcsicmp(L"DIB", pExt) == 0
		|| _wcsicmp(L"RLE", pExt) == 0)
	{
		GetEncoderClsid(L"image/bmp", imgClsid);
	}
	else if (_wcsicmp(L"JPEG", pExt) == 0
		|| _wcsicmp(L"JPG", pExt) == 0
		|| _wcsicmp(L"JPE", pExt) == 0
		|| _wcsicmp(L"JFIF", pExt) == 0)
	{
		GetEncoderClsid(L"image/jpeg", imgClsid);
	}
	else if (_wcsicmp(L"GIF", pExt) == 0)
	{
		GetEncoderClsid(L"image/gif", imgClsid);
	}
	else if (_wcsicmp(L"TIFF", pExt) == 0
		|| _wcsicmp(L"TIF", pExt) == 0)
	{
		GetEncoderClsid(L"image/tiff", imgClsid);
	}
	else if (_wcsicmp(L"PNG", pExt) == 0)
	{
		GetEncoderClsid(L"image/png", imgClsid);
	}
	m_pBitmap->Save(szFilePath, &imgClsid);
}